﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

namespace Moving03.Administrator
{
    
    public partial class WebForm16 : System.Web.UI.Page
    {
        DataClasses1DataContext context = new DataClasses1DataContext();
        ArrayList arr = new ArrayList();
        int ma = 0;
        int stt = 1;
        ArrayList maso = new ArrayList();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (User.Identity.IsAuthenticated == false || User.Identity.IsAuthenticated == true && !User.IsInRole("Admin"))
            {
                Response.Redirect("../Default.aspx");
            }
                    int ma = int.Parse(Request.QueryString["ID"]);
                    Session["ma"] = ma;
                    var query = from ddtg in context.DiaDiemTrungGian1Tuyens
                                where ddtg.Id_CPDDNNgoai == ma
                                select ddtg;
                    foreach (var ddtg in query)
                    {
                        arr.Add(ddtg.Id_DDTG1T);
                    }
                    Panel1.Controls.Add(new LiteralControl("<table  border='0'  width='100%' cellpadding='0' cellspacing='1'>"));
                    Panel1.Controls.Add(new LiteralControl("<tr bgcolor='#507DD2' style='color: #FFFFFF; font-size:14px; height:40px' align='center' >"));
                    Panel1.Controls.Add(new LiteralControl("<th>STT</th>"));
                    Panel1.Controls.Add(new LiteralControl("<th>Chọn</th>"));
                    Panel1.Controls.Add(new LiteralControl("<th>Địa Điểm</th>"));
                    Panel1.Controls.Add(new LiteralControl("<th>Số Thứ Tự</th>"));
                    Panel1.Controls.Add(new LiteralControl("</tr>"));

                    var query2 = from dd in context.DiaDiemTrungGians
                                 select dd;

                    int thutu = 1;
                    foreach (var dd in query2)
                    {
                        CheckBox ckcheck = new CheckBox();
                        ckcheck.ID = "ck_" + stt;
                        ckcheck.Checked = false;
                        ckcheck.AutoPostBack = true;
                        ckcheck.CheckedChanged+= new EventHandler(ckcheck_CheckedChanged);

                        TextBox tb = new TextBox();

                        tb.ID = "txt_" + stt;
                        
                        tb.Enabled = false;

                        if (kiemTraTonTai(dd.Id_DDTrungGian))
                        {

                            maso.Add(dd.Id_DDTrungGian);
                            ckcheck.Checked = true;
                            int mama = int.Parse(dd.Id_DDTrungGian.ToString());
                            tb.Text = thuTukiemTraTonTai(mama).ToString();
                            tb.Enabled = true;
                        }

                        Label lb = new Label();
                        lb.ID = "lb_" + stt;
                        lb.Visible = false;
                        lb.Text = dd.Id_DDTrungGian.ToString();
                        Panel1.Controls.Add(lb);

                        Panel1.Controls.Add(new LiteralControl("<tr style='height:30px'>"));
                        Panel1.Controls.Add(new LiteralControl("<td>" + stt));
                        Panel1.Controls.Add(new LiteralControl("</td>"));

                        Panel1.Controls.Add(new LiteralControl("<td>"));
                        Panel1.Controls.Add(ckcheck);
                        Panel1.Controls.Add(new LiteralControl("</td>"));

                        Panel1.Controls.Add(new LiteralControl("<td>" + dd.TenDiaDiem));
                        Panel1.Controls.Add(new LiteralControl("</td>"));

                        Panel1.Controls.Add(new LiteralControl("<td>"));
                       
                        Panel1.Controls.Add(tb);
                        Panel1.Controls.Add(new LiteralControl("</td>"));
                        Panel1.Controls.Add(new LiteralControl("</tr>"));
                        stt++;

                    }
                    Panel1.Controls.Add(new LiteralControl("</table>"));
                    Button capnhat = new Button();
                    capnhat.Text = "Cập Nhật";
                    capnhat.Click += new EventHandler(capnhat_Click);
                    Panel1.Controls.Add(capnhat);

                   
                    Button trove = new Button();
                    trove.Text = "Trở Về";
                    trove.Click += new EventHandler(Trove_Click);
                    Panel1.Controls.Add(trove);
        }
        protected void ckcheck_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox ck = new CheckBox();
            ck = (CheckBox)sender;
            string s = ck.ID;
            string[] split = s.Split('_');
            string idma = split[1];
            int ima = int.Parse(idma);

            TextBox tb = new TextBox();
            string matextbox = "txt_" + idma;
            tb = Panel1.FindControl(matextbox) as TextBox;
            if (ck.Checked)
                tb.Enabled = true;
            else
            {
                tb.Enabled = false;
                tb.Text = "";
            }
        }
       
        protected void Trove_Click(object sender, EventArgs e)
        {
            Response.Redirect("Quanlydiadiemtrunggian1tuyen.aspx");
        }
        protected void capnhat_Click(object sender, EventArgs e)
        {

            for (int i = 1; i < stt ; i++)
            { 
                CheckBox ckbox = new CheckBox();
                string id_checkcbox = "ck_" + (i).ToString();
                ckbox = Panel1.FindControl(id_checkcbox) as CheckBox;
                Label lb = new Label();
                string idlb = "lb_" + i;
                lb = Panel1.FindControl(idlb) as Label;
                string id = lb.Text;

                

                if (ckbox.Checked == true)
                {
                    TextBox txtbox = new TextBox();
                    string idtextbox = "txt_" + (i).ToString();
                    txtbox = Panel1.FindControl(idtextbox) as TextBox;

                    if (!kiemTraTonTai(int.Parse(id)))
                    {
                        DiaDiemTrungGian1Tuyen ddtg1t = new DiaDiemTrungGian1Tuyen();
                        ddtg1t.Id_TG = int.Parse(id);
                        ddtg1t.Id_CPDDNNgoai = int.Parse(Session["ma"].ToString());
                        ddtg1t.thutuvanchuyen = int.Parse(txtbox.Text);
                        context.DiaDiemTrungGian1Tuyens.InsertOnSubmit(ddtg1t);
                        context.SubmitChanges();
                    }
                    else
                    {                        
                      
                        DiaDiemTrungGian1Tuyen ddtg = context.DiaDiemTrungGian1Tuyens.Single(p => p.Id_TG == int.Parse(id) && p.Id_CPDDNNgoai ==int.Parse(Session["ma"].ToString()));
                        ddtg.thutuvanchuyen = int.Parse(txtbox.Text);
                        context.SubmitChanges();
                        
                    }
               }
                else
                {

                    if (kiemTraTonTai(int.Parse(id)))
                    {
                        int ma = int.Parse(id);
                        int ma2 = int.Parse(Session["ma"].ToString());
                        DiaDiemTrungGian1Tuyen ddtg1t = new DiaDiemTrungGian1Tuyen();
                        ddtg1t = context.DiaDiemTrungGian1Tuyens.Single(p => p.Id_TG ==ma && p.Id_CPDDNNgoai==ma2 ); 
                        context.DiaDiemTrungGian1Tuyens.DeleteOnSubmit(ddtg1t);
                    }
                    context.SubmitChanges();
                }

            }
        }
        public bool kiemTraTonTai(int ma)
        {
            for (int i = 0; i < arr.Count; i++)
            {
                  //Product product = db.Products.Single(p => p.ProductID == productID);
                DiaDiemTrungGian1Tuyen ddtg = context.DiaDiemTrungGian1Tuyens.Single(p => p.Id_DDTG1T == (int)arr[i]);
                if (ma == ddtg.Id_TG)
                    return true;
            }
            return false;
        }

        public int thuTukiemTraTonTai(int ma)
        {
            int kq=0;
            for (int i = 0; i < arr.Count; i++)
            {
                //Product product = db.Products.Single(p => p.ProductID == productID);
                DiaDiemTrungGian1Tuyen ddtg = context.DiaDiemTrungGian1Tuyens.Single(p => p.Id_DDTG1T == (int)arr[i]);
                if (ma == ddtg.Id_TG)
                {
                    kq = (int)ddtg.thutuvanchuyen;
                }
            }
            return kq;
        }
             
    }
}